"""
获取百度贴吧源代码并保存
"""
import requests
from urllib.parse import urlencode
class New_url(object):
    def __init__(self):
        self.url='https://tieba.baidu.com/p/7138577846?'
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
        }
    def eam(self,nam):
        params = {
            'pn': nam
        }
        New_urll=self.url+urlencode(params)
        response=requests.get(New_urll, headers=self.headers)
        if response.status_code == 200:
            response.encoding='utf-8'
            return response.text
        else:
            return None
    def bc(self,resp,nam):
        nem=f'实战第{nam}页保存成功.html'
        with open(nem,'w',encoding='utf-8') as f:
            f.write(resp)
if __name__ == '__main__':
    a=New_url()
    for nam in range(1,4):
        a.eam(nam)
        iam=a.eam(nam)
        a.bc(resp=iam,nam=nam)